const express = require('express');
const operation = require('../controller/operation')
const router = express.Router();
// 添加场地
router.post('/add',(req,res)=>{
    let data = {cname,price,sid,state,startTime,endTime} = req.body;
    operation.addCourt(data).then(ok =>{
        res.json({
            status:201,
            msg:'场地添加成功'
        })
    },err=>{
        res.json({
            status:400,
            msg:'场地添加失败'
        })
    })
})
//获取场地信息(分页)
router.get('/pagination',(req,res)=>{
    let currentPage = parseInt(req.query.currentPage) || 1;
    let pageSize = parseInt(req.query.pageSize) || 2;
    let offset = (currentPage - 1)*pageSize;
})
//获取场地信息
router.get('/getInfo',(req,res)=>{
    if(req.query.sid !== 'undefined'){
        console.log(req.query);
        operation.getCourtBySid(req.query.sid).then(data=>{
            res.json({
                status:200,
                data,
                msg:'获取场地信息成功'
            })
        },err=>{
            res.json({
                status:400,
                msg:'获取场地信息失败'
            })
        })
    }else{
        operation.selectAllMuti('cid,court.sid,cname,price,state,sname,startTime,endTime,sname','sid','stadium','court').then(data=>{
            res.json({
                status:200,
                data,
                msg:'获取场地信息成功'
            })
        },err=>{
            res.json({
                status:400,
                msg:'获取场地信息失败'
            })
        })
    }

})
// 删除球场
router.delete('/remove',(req,res)=>{
    let {cid} = req.query;
    
    operation.removeCourt(cid).then(result=>{
        res.json({
            status:200,
            msg:result
        })
    },err=>{
        res.json({
            status:400,
            msg:err
        })
    })
})
// 更新场地信息
router.put('/edit',(req,res)=>{
    let data={cid,cname,price,state,startTime,endTime} = req.body;
    
    operation.editCourt(data).then(result=>{
        res.json({
            status:200,
            msg:result
        })
    },err=>{
        res.json({
            status:400,
            msg:err
        })
    })
})
// 球场模糊查询
router.get('/search',(req,res)=>{
    let {query} = req.query;
    operation.searchCourt(query).then(result=>{
        res.json({
            status:200,
            data:result,
            msg:'查询成功'
        })
    },err=>{
        res.json({
            status:400,
            msg:'查询失败'
        })
    })
})


module.exports = router;